#include "alertwindow.h"
#include "ui_alertwindow.h"
#include <QTimer>

AlertWindow::AlertWindow(QWidget *parent, QString message, int time) :
    QWidget(parent),
    ui(new Ui::AlertWindow)
{
    ui->setupUi(this);

    time_ = time;
    ui->message->setText( message);
    ui->elapsed->setMaximum(time_ * 1000);
    ui->elapsed->setValue(time_);

    show();
    QTimer* t = new QTimer(this);
    connect(t, SIGNAL(timeout()), SLOT(timeout()));
    t->start(1000);

    t = new QTimer(this);
    connect(t, SIGNAL(timeout()), SLOT(timeout2()));
    t->start(100);

}

void AlertWindow::timeout()
{
    time_--;
    if (time_ <= 0){
        hide();
        emit timeup();
    }
}

void AlertWindow::timeout2()
{
    ui->elapsed->setValue(ui->elapsed->value() + 100);
}

AlertWindow::~AlertWindow()
{
    delete ui;
}

void AlertWindow::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
